#include "G_GraphicesTest.h"
#include "Gscsd.h"
#include "G_u8g2.h"
#include "stdbool.h"

void u8g2_prepare(void) {
  GU8g2_setFont(u8g2_font_6x10_tf);
	GU8g2_SetFontRefHeightExtendedText();
  GU8g2_setDrawColor(1);
	GU8g2_SetFontPosTop();
  GU8g2_SetFontDirection(0);
}

void u8g2_box_frame(uint8_t a) {
  GU8g2_drawStr( 0, 0, "drawBox");
  GU8g2_drawBox(5,10,20,10);
  GU8g2_drawBox(10+a,15,30,7);
  GU8g2_drawStr( 0, 30, "drawFrame");
  GU8g2_drawFrame(5,10+30,20,10);
  GU8g2_drawFrame(10+a,15+30,30,7);
}

void u8g2_disc_circle(uint8_t a) {
  GU8g2_drawStr( 0, 0, "drawDisc");
  GU8g2_drawDisc(10,18,9,U8G2_DRAW_ALL);
  GU8g2_drawDisc(24+a,16,7,U8G2_DRAW_ALL);
  GU8g2_drawStr( 0, 30, "drawCircle");
  GU8g2_drawCircle(10,18+30,9,U8G2_DRAW_ALL);
  GU8g2_drawCircle(24+a,16+30,7,U8G2_DRAW_ALL);
}

void u8g2_r_frame(uint8_t a) {
  GU8g2_drawStr( 0, 0, "drawRFrame/Box");
  GU8g2_drawRFrame(5, 10,40,30, a+1);
  GU8g2_drawRBox(50, 10,25,40, a+1);
}

void u8g2_string(uint8_t a) {
  GU8g2_SetFontDirection(0);
  GU8g2_drawStr(30+a,31, " 0");
  GU8g2_SetFontDirection(1);
  GU8g2_drawStr(30,31+a, " 90");
  GU8g2_SetFontDirection(2);
  GU8g2_drawStr(30-a,31, " 180");
  GU8g2_SetFontDirection(3);
  GU8g2_drawStr(30,31-a, " 270");
}

void u8g2_line(uint8_t a) {
  GU8g2_drawStr( 0, 0, "drawLine");
  GU8g2_drawLine(7+a, 10, 40, 55);
  GU8g2_drawLine(7+a*2, 10, 60, 55);
  GU8g2_drawLine(7+a*3, 10, 80, 55);
  GU8g2_drawLine(7+a*4, 10, 100, 55);
}

void u8g2_triangle(uint8_t a) {
  uint16_t offset = a;
  GU8g2_drawStr( 0, 0, "drawTriangle");
  GU8g2_drawTriangle(14,7, 45,30, 10,40);
  GU8g2_drawTriangle(14+offset,7-offset, 45+offset,30-offset, 57+offset,10-offset);
  GU8g2_drawTriangle(57+offset*2,10, 45+offset*2,30, 86+offset*2,53);
  GU8g2_drawTriangle(10+offset,40+offset, 45+offset,30+offset, 86+offset,53+offset);
}

void u8g2_ascii_1() {
  char s[2] = " ";
  uint8_t x, y;
  GU8g2_drawStr( 0, 0, "ASCII page 1");
  for( y = 0; y < 6; y++ ) {
    for( x = 0; x < 16; x++ ) {
      s[0] = y*16 + x + 32;
      GU8g2_drawStr(x*7, y*10+10, s);
    }
  }
}

void u8g2_ascii_2() {
  char s[2] = " ";
  uint8_t x, y;
  GU8g2_drawStr( 0, 0, "ASCII page 2");
  for( y = 0; y < 6; y++ ) {
    for( x = 0; x < 16; x++ ) {
      s[0] = y*16 + x + 160;
      GU8g2_drawStr(x*7, y*10+10, s);
    }
  }
}

void u8g2_extra_page(uint8_t a)
{
  GU8g2_drawStr( 0, 0, "Unicode");
	GU8g2_setFont(u8g2_font_unifont_t_chinese2);
	GU8g2_drawUTF8(0, 0, "你好");
  GU8g2_setFont(u8g2_font_unifont_t_symbols);
  GU8g2_SetFontPosTop();
  GU8g2_drawUTF8(0, 24, "☀ ☁");
  switch(a) {
    case 0:
    case 1:
    case 2:
    case 3:
      GU8g2_drawUTF8(a*3, 36, "☂");
      break;
    case 4:
    case 5:
    case 6:
    case 7:
      GU8g2_drawUTF8(a*3, 36, "☔");
      break;
  }
}

#define cross_width 24
#define cross_height 24
static const unsigned char cross_bits[] U8X8_PROGMEM  = {
  0x00, 0x18, 0x00, 0x00, 0x24, 0x00, 0x00, 0x24, 0x00, 0x00, 0x42, 0x00, 
  0x00, 0x42, 0x00, 0x00, 0x42, 0x00, 0x00, 0x81, 0x00, 0x00, 0x81, 0x00, 
  0xC0, 0x00, 0x03, 0x38, 0x3C, 0x1C, 0x06, 0x42, 0x60, 0x01, 0x42, 0x80, 
  0x01, 0x42, 0x80, 0x06, 0x42, 0x60, 0x38, 0x3C, 0x1C, 0xC0, 0x00, 0x03, 
  0x00, 0x81, 0x00, 0x00, 0x81, 0x00, 0x00, 0x42, 0x00, 0x00, 0x42, 0x00, 
  0x00, 0x42, 0x00, 0x00, 0x24, 0x00, 0x00, 0x24, 0x00, 0x00, 0x18, 0x00, };

#define cross_fill_width 24
#define cross_fill_height 24
static const unsigned char cross_fill_bits[] U8X8_PROGMEM  = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x18, 0x64, 0x00, 0x26, 
  0x84, 0x00, 0x21, 0x08, 0x81, 0x10, 0x08, 0x42, 0x10, 0x10, 0x3C, 0x08, 
  0x20, 0x00, 0x04, 0x40, 0x00, 0x02, 0x80, 0x00, 0x01, 0x80, 0x18, 0x01, 
  0x80, 0x18, 0x01, 0x80, 0x00, 0x01, 0x40, 0x00, 0x02, 0x20, 0x00, 0x04, 
  0x10, 0x3C, 0x08, 0x08, 0x42, 0x10, 0x08, 0x81, 0x10, 0x84, 0x00, 0x21, 
  0x64, 0x00, 0x26, 0x18, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };

#define cross_block_width 14
#define cross_block_height 14
static const unsigned char cross_block_bits[] U8X8_PROGMEM  = {
  0xFF, 0x3F, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 
  0xC1, 0x20, 0xC1, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 0x01, 0x20, 
  0x01, 0x20, 0xFF, 0x3F, };

void u8g2_bitmap_overlay(uint8_t a) {
  uint8_t frame_size = 28;

  GU8g2_drawStr(0, 0, "Bitmap overlay");

  GU8g2_drawStr(0, frame_size + 12, "Solid / transparent");
  GU8g2_setBitmapMode(false /* solid */);
  GU8g2_drawFrame(0, 10, frame_size, frame_size);
  GU8g2_drawXBMP(2, 12, cross_width, cross_height, cross_bits);
  if(a & 4)
    GU8g2_drawXBMP(7, 17, cross_block_width, cross_block_height, cross_block_bits);

  GU8g2_setBitmapMode(true /* transparent*/);
  GU8g2_drawFrame(frame_size + 5, 10, frame_size, frame_size);
  GU8g2_drawXBMP(frame_size + 7, 12, cross_width, cross_height, cross_bits);
  if(a & 4)
    GU8g2_drawXBMP(frame_size + 12, 17, cross_block_width, cross_block_height, cross_block_bits);
}

void u8g2_bitmap_modes(uint8_t transparent) {
  const uint8_t frame_size = 24;

  GU8g2_drawBox(0, frame_size * 0.5, frame_size * 5, frame_size);
  GU8g2_drawStr(frame_size * 0.5, 50, "Black");
  GU8g2_drawStr(frame_size * 2, 50, "White");
  GU8g2_drawStr(frame_size * 3.5, 50, "XOR");
  
  if(!transparent) {
    GU8g2_setBitmapMode(false /* solid */);
    GU8g2_drawStr(0, 0, "Solid bitmap");
  } else {
    GU8g2_setBitmapMode(true /* transparent*/);
    GU8g2_drawStr(0, 0, "Transparent bitmap");
  }
  GU8g2_setDrawColor(0);// Black
  GU8g2_drawXBMP(frame_size * 0.5, 24, cross_width, cross_height, cross_bits);
  GU8g2_setDrawColor(1); // White
  GU8g2_drawXBMP(frame_size * 2, 24, cross_width, cross_height, cross_bits);
  GU8g2_setDrawColor(2); // XOR
  GU8g2_drawXBMP(frame_size * 3.5, 24, cross_width, cross_height, cross_bits);
}

uint8_t draw_state = 0;

void draw(void) {
  u8g2_prepare();
  switch(draw_state >> 3) {
    case 0: u8g2_box_frame(draw_state&7); break;
    case 1: u8g2_disc_circle(draw_state&7); break;
    case 2: u8g2_r_frame(draw_state&7); break;
    case 3: u8g2_string(draw_state&7); break;
    case 4: u8g2_line(draw_state&7); break;
    case 5: u8g2_triangle(draw_state&7); break;
    case 6: u8g2_ascii_1(); break;
    case 7: u8g2_ascii_2(); break;
    case 8: u8g2_extra_page(draw_state&7); break;
    case 9: u8g2_bitmap_modes(0); break;
    case 10: u8g2_bitmap_modes(1); break;
    case 11: u8g2_bitmap_overlay(draw_state&7); break;
  }
}

void G_GraphicesTest(void){
	// picture loop  
  GU8g2_clearBuffer();
  draw();
  GU8g2_sendBuffer();
  
  // increase the state
  draw_state++;
  if ( draw_state >= 12*8 )
    draw_state = 0;

  // deley between each page
  delay_ms(100);

}
